const mysql = require('../db/mysql')
const userService = require('../service/userService')


// 查询用户列表
exports.getUserList = function (req, res) {
    let { userName, phoneNumber, page } = req.query
    phoneNumber = `%${phoneNumber}%`
    userName = `%${userName}%`
    mysql.query(userService.userList, [phoneNumber, userName, (page - 1) * 10]).then((data) => {
        mysql.query(userService.totalCount, [phoneNumber, userName]).then((total) => {
            const jsonData = JSON.parse(JSON.stringify(data))
            res.json({
                code: 0,
                total: total[0].total,
                data: jsonData,
            })
        })
    })
}

// 根据token获取当前用户信息
exports.getUserInfo = function (req, res) {
    mysql.query(userService.userInfo, req.auth.username).then((data) => {
        const jsonData = JSON.parse(JSON.stringify(data))
        res.json({
            code: 0,
            data: jsonData[0],
        })
    })
}

// 新增用户
exports.addUser = function (req, res) {
    let { userName, password, phoneNumber, roles } = req.body
    mysql.query(userService.findExists, [userName, phoneNumber]).then((exists) => {
        const existsList = JSON.parse(JSON.stringify(exists))
        if(existsList.length) {
            res.json({
                code: 1,
                message: '用户名或手机号已存在',
            })
        } else {
            const roleStr = JSON.stringify(roles)
            mysql.query(userService.addUser, [userName, password, phoneNumber, roleStr]).then((data) => {
                const jsonData = JSON.parse(JSON.stringify(data))
                res.json({
                    code: 0,
                    data: jsonData[0],
                })
            })
        }
    })
}

// 编辑用户
exports.editUser = function (req, res) {
    let { userName, phoneNumber, roles, id } = req.body
    const roleStr = JSON.stringify(roles)
    mysql.query(userService.editUser, [userName, phoneNumber, roleStr, id]).then((data) => {
        const jsonData = JSON.parse(JSON.stringify(data))
        res.json({
            code: 0,
            data: jsonData[0],
        })
    })
}

// 删除
exports.deleteUser = function (req, res) {
    let { id } = req.body
    mysql.query(userService.deleteUser, id).then((data) => {
        const jsonData = JSON.parse(JSON.stringify(data))
        res.json({
            code: 0,
            data: jsonData[0],
        })
    })
}

// 修改密码
exports.password = function (req, res) {
    const userName = req.auth.username
    let { oldPassword, newPassword } = req.body
    mysql.query(userService.findExists, [userName, '']).then((users) => {
        const userList = JSON.parse(JSON.stringify(users))
        const user = userList[0]
        if(user.user_password === oldPassword) {
            mysql.query(userService.changePassword, [newPassword, user.user_id]).then((data) => {
                const jsonData = JSON.parse(JSON.stringify(data))
                res.json({
                    code: 0,
                    data: jsonData[0],
                })
            })
        } else {
            res.json({
                code: 1,
                message: '旧密码错误',
            })
        }
    })
}
